<?php
/**
 * 
 * @author yuxun
 *
 */

class EveryGradeService extends Service {

    public static function gradeHeader($params){
        $header = array(
                'Level'   => array('text' => PubFunc::t('等级')),
        		'NowLevelNum'   => array('text' => PubFunc::t('当前等级角色总个数')),
        		'd_level'   => array('text' => PubFunc::t('大于等于本级人数')),
        		'x_level'   => array('text' => PubFunc::t('1级至本级人数')),
        		'all_level'   => array('text' => PubFunc::t('所有等级总数')),
        		'LossRate'   => array('text' => PubFunc::t('流失率')),
        		'LevelProportion'   => array('text' => PubFunc::t('等级比重')),
        		'TotalLevelProportion'   => array('text' => PubFunc::t('累计等级比重')),
        		'NowLevelOnNum'   => array('text' => PubFunc::t('当前等级在线人数')),
        		'NowLevelOffNum'   => array('text' => PubFunc::t('当前等级离线人数')),
        		'ExcludeOnLossRate'   => array('text' => PubFunc::t('排除在线流失率')),
        		'ThreeLevelOffNum'   => array('text' => PubFunc::t('三天未在线人数')),
        		'ThreeLossRate'   => array('text' => PubFunc::t('三天流失率')),
        		'OneLevelOffNum'   => array('text' => PubFunc::t('24小时未在线人数')),
        		'OneLossRate'   => array('text' => PubFunc::t('24小时流失率')),
        );
        return $header;
    }
    
    public static function gradeData($params){
    	$data = ModEveryGrade::indexData($params);
    	
    	$new_data = array();
    	foreach ($data as $key=>$val){
    		$new_data[$val['Level']]['Level'] = $val['Level'];
    		$new_data[$val['Level']]['NowLevelNum'] += $val['NowLevelNum'];
    		$new_data[$val['Level']]['NowLevelOnNum'] += $val['NowLevelOnNum'];
    		$new_data[$val['Level']]['NowLevelOffNum'] += $val['NowLevelOffNum'];
    	}
    	 
    	$c_data = array();
    	foreach ($new_data as $key=>$val){
    		$now_level = $val['Level'];
    		$d_level = 0;
    		$x_level = 0;
    		foreach ($new_data as $k=>$v){
    			if($now_level <= $v['Level']){
    				$d_level += $v['NowLevelNum'];
    			}
    			if($now_level >= $v['Level']){
    				$x_level += $v['NowLevelNum'];
    			}
    		}
    		$c_data[$now_level]['d_level'] = $d_level;
    		$c_data[$now_level]['x_level'] = $x_level;
    	}
    	
//     	echo '<pre>';
//     	var_dump($c_data);exit;
    	
    	 
    	$f_data = array();
    	foreach ($data as $key=>$val){
    		$f_data[$val['Level']][$val['IsOnline']][$val['Rate']]['Level'] = $val['Level'];
    		$f_data[$val['Level']][$val['IsOnline']][$val['Rate']]['IsOnline'] = $val['IsOnline'];
    		$f_data[$val['Level']][$val['IsOnline']][$val['Rate']]['Rate'] = $val['Rate'];
    		$f_data[$val['Level']][$val['IsOnline']][$val['Rate']]['NowLevelNum'] += $val['NowLevelNum'];
    	}
    	
    	
    	$all_level = 0; //所有等级人数
    	$fin_data = array();
    	foreach ($f_data as $ko=>$vo){
//     		echo '等级=='.$ko;
//     		echo '<br/>';
//     		echo '<pre>';   		
//     		var_dump($vo);
//     		echo '<br/>';echo '.............................................';echo '<br/>';
    		$NowLevelNum = 0;  //当前等级全部人数
    		$NowLevelOnNum = 0; //当前等级在线人数
    		$NowLevelOffNum = 0; //当前等级离线人数
    		$OneLevelOffNum = 0;  //24小时未在线人数
    		$ThreeLevelOffNum = 0; //3天未在线人数
    		foreach ($vo as $kt=>$vt){
//     			echo '<pre>';
//     			var_dump($vt);
//     			echo '<br/>';echo '许佳文.............................................许佳文';echo '<br/>';
    			foreach ($vt as $ks=>$vs){
    				$all_level += $vs['NowLevelNum'];
    				$NowLevelNum += $vs['NowLevelNum'];
    				if($vs['IsOnline'] == 0){
    					$NowLevelOffNum += $vs['NowLevelNum'];
    				}
    				if($vs['IsOnline'] == 1){
    					$NowLevelOnNum += $vs['NowLevelNum'];
    				}
    				
    				if($vs['Rate'] >= 1){
    					$OneLevelOffNum += $vs['NowLevelNum'];
    				}
    				
    				if($vs['Rate'] >= 3){
    					$ThreeLevelOffNum += $vs['NowLevelNum'];
    				}
    				
//     				echo '<pre>';
//     				var_dump($vs);
//     				echo '<br/>';echo '雨欣.............................................雨欣';echo '<br/>';
    			}
    		}
    		$d = array();
    		$d['Level'] = $ko;
    		$d['NowLevelNum'] = $NowLevelNum;
    		$d['NowLevelOnNum'] = $NowLevelOnNum;
    		$d['NowLevelOffNum'] = $NowLevelOffNum;
    		$d['OneLevelOffNum'] = $OneLevelOffNum;
    		$d['ThreeLevelOffNum'] = $ThreeLevelOffNum;
    		$d['d_level'] = $c_data[$ko]['d_level'];
    		$d['x_level'] = $c_data[$ko]['x_level'];

//     		echo '<br/>';
//     		echo $NowLevelNum;echo '<br/>';
//     		echo $NowLevelOnNum;echo '<br/>';
//     		echo $NowLevelOffNum;echo '<br/>';
//     		echo $OneLevelOffNum;echo '<br/>';
//     		echo $d['d_level'];echo '<br/>';
//     		echo $d['x_level'];echo '<br/>';
    		array_push($fin_data, $d);
    		
//     		exit;
    	}
    	 
//     	echo $all_level;
//     	echo '<pre>';
//     	var_dump($fin_data);
//     	exit;
    	
    	foreach($fin_data as &$v){
    		//$v['NowLevelNum'] = $v['NowLevelNum'].'-'.$v['d_level'].'-'.$v['x_level'].'-'.$all_level;
    	
    		$v['all_level'] = $all_level;
    		//流失率
    		$v['LossRate'] = number_format($v["NowLevelNum"] / $v['d_level'] * 100, 2, '.', '')."%";
    		//等级比重
    		$v['LevelProportion'] = number_format($v["NowLevelNum"] / $all_level * 100, 2, '.', '')."%";
    		//累计等级比重
    		$v['TotalLevelProportion'] = number_format($v["x_level"] / $all_level * 100, 2, '.', '')."%";
    		//排除在线流失率
    		$v['ExcludeOnLossRate'] = number_format($v["NowLevelOffNum"] / $v['d_level'] * 100, 2, '.', '')."%";
    		//24小时流失率
    		$v['OneLossRate'] = number_format($v["OneLevelOffNum"] / $v['d_level'] * 100, 2, '.', '')."%";
    		//3天流失率
    		$v['ThreeLossRate'] = number_format($v["ThreeLevelOffNum"] / $v['d_level'] * 100, 2, '.', '')."%";

    	}
    	unset($v);
    	
    	//二维数组按字段Level排序
    	foreach ($fin_data as $key => $row){
    		$level[$key]  = $row['Level'];
    	}
    	array_multisort($level, SORT_ASC ,$fin_data);
    	
//     	echo '<pre>';
//     	var_dump($fin_data);exit;
    	
    	return $fin_data;
    }
    
    /**
     * 暂废弃的方法
     * @param unknown $params
     * @return multitype:
     */
    public static function gradeData1($params){  
    	$data = ModEveryGrade::indexData($params);
    	
    	$new_data = array();
    	foreach ($data as $key=>$val){
    		$new_data[$val['Level']]['Level'] = $val['Level'];
    		$new_data[$val['Level']]['NowLevelNum'] += $val['NowLevelNum'];
    		$new_data[$val['Level']]['NowLevelOnNum'] += $val['NowLevelOnNum'];
    		$new_data[$val['Level']]['NowLevelOffNum'] += $val['NowLevelOffNum'];
    	}
    	
    	$c_data = array();
    	$all_level = 0;
    	foreach ($new_data as $key=>$val){
    		$d = array();
    		$d['Level'] = $val['Level'];
    		$d['NowLevelNum'] = $val['NowLevelNum'];
    		$d['NowLevelOnNum'] = $val['NowLevelOnNum'];
    		$d['NowLevelOffNum'] = $val['NowLevelOffNum'];
    		$now_level = $val['Level'];
    		$d_level = 0;
    		$x_level = 0;
    		foreach ($new_data as $k=>$v){
    			if($now_level <= $v['Level']){
    				$d_level += $v['NowLevelNum'];
    			}
    			if($now_level >= $v['Level']){
    				$x_level += $v['NowLevelNum'];
    			}
    		}
    		$d['d_level'] = $d_level;
    		$d['x_level'] = $x_level;
    		
    		$all_level += $val['NowLevelNum'];
    		array_push($c_data, $d);
    	}
    	
    	foreach($c_data as &$v){
            //$v['NowLevelNum'] = $v['NowLevelNum'].'-'.$v['d_level'].'-'.$v['x_level'].'-'.$all_level;          
            
            //流失率 
            $v['LossRate'] = number_format($v["NowLevelNum"] / $v['d_level'] * 100, 2, '.', '')."%";
            //等级比重
            $v['LevelProportion'] = number_format($v["NowLevelNum"] / $all_level * 100, 2, '.', '')."%";
            //累计等级比重            
            $v['TotalLevelProportion'] = number_format($v["x_level"] / $all_level * 100, 2, '.', '')."%";
            
            $v['NowLevelOffNum'] = '';
        }
        unset($v);
    	
    	//var_dump($c_data);
    	
    	return $c_data;
    }

    

    
    
} 